package jmind.core.http.selenium;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.safari.SafariDriver;

/**
 * selenium
 https://blog.csdn.net/qq_22003641/article/details/79137327
 * selenium  自动化测试模拟浏览器
 * https://blog.csdn.net/qq_22003641/article/details/79137327
 *
 * https://blog.csdn.net/qq_36853469/article/details/103234466
 *
 * firefox驱动
 * https://npm.taobao.org/mirrors/geckodriver/v0.29.0/
 * chrome驱动
 * https://npm.taobao.org/mirrors/chromedriver
 *
 * http://selenium-release.storage.googleapis.com/index.html
 * https://www.cnblogs.com/meiling-ji/p/5283476.html
 *   mac 启动 SafariDriver
 *   https://blog.csdn.net/zczy01234/article/details/85792410
 *   需要在Safari工具栏的“开发”中，勾选“允许远程自动化”；
 *
 *  js抓取工具  webster
 *  https://github.com/zhuyingda/webster?spm=ata.13261165.0.0.34b3537frFRCKC
 * @author weibo.xie
 */
public class BrowserDriverFactory {

    public static enum Driver {
        firefox("webdriver.firefox.marionette"),
        safari("webdriver.safari.driver"),
        ie("webdriver.ie.driver"),
        chrome("webdriver.chrome.driver"),
        edge("webdriver.edge.driver"),
        opera("webdriver.opera.driver");


        String name;

        Driver(String name) {
            this.name = name;
        }
    }

    /**
     * 根据驱动类型，创建webDiver工厂
     * @param driver
     * @param driverPath
     * @return
     */
    public static WebDriver createDriver(Driver driver, String driverPath) {
        System.setProperty(driver.name, driverPath);
        WebDriver webDriver=null;
        switch (driver) {
            case chrome:
                webDriver = new ChromeDriver();
                break;
            case firefox:
                webDriver=new FirefoxDriver();
                break;
            case ie:
                webDriver=new InternetExplorerDriver();
                break;
            case safari:
                System.setProperty("webdriver.safari.noinstall", "true");
                webDriver=new SafariDriver();
                break;
            case edge:
                webDriver=new EdgeDriver();
                break;
            case opera:
                webDriver=new OperaDriver();
                break;
        }

        return webDriver;
    }

    /**
     * 某个元素截屏
     * @param webElement
     * @param outputType
     * @param <X>
     * @return
     * @throws WebDriverException
     */
    public static  <X> X getScreenshotAs(WebElement webElement, OutputType<X> outputType) throws WebDriverException{
        return webElement.getScreenshotAs(outputType);
    }
}
